﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Lswweb.IDAL;
using Lswweb.Model;

namespace Lswweb.SqlServerDAL
{
    public class CommentaryDAL : ICommentary
    {
        private Builder<CommentaryInfo> builderDAL = new Builder<CommentaryInfo>("Commentary", "Id");

        public CommentaryDAL()
        {
            builderDAL.GetParameters += new ParametersBuilder<CommentaryInfo>(builderDAL_GetParameters);
            builderDAL.CreateModel += new ReaderBind<CommentaryInfo>(builderDAL_CreateModel);
        }

        CommentaryInfo builderDAL_CreateModel(Hashtable reader)
        {
            CommentaryInfo info = new CommentaryInfo();

            if (reader.ContainsKey("Id"))
                info.Id = Convert.ToInt32(reader["Id"]);

            if (reader.ContainsKey("UserId"))
                info.UserId = Convert.ToInt32(reader["UserId"]);

            if (reader.ContainsKey("ProductId"))
                info.ProductId = Convert.ToInt32(reader["ProductId"]);

            if (reader.ContainsKey("UserName"))
                info.UserName = reader["UserName"].ToString();

            if (reader.ContainsKey("Content"))
                info.Content = reader["Content"].ToString();

            if (reader.ContainsKey("FeedBack"))
                info.FeedBack = reader["FeedBack"].ToString();

            if (reader.ContainsKey("DateCreated"))
                info.DateCreated = Convert.ToDateTime(reader["DateCreated"]);

            if (reader.ContainsKey("ProductName"))
                info.ProductName = reader["ProductName"].ToString();

            return info;
        }

        IDataParameter[] builderDAL_GetParameters(CommentaryInfo t)
        {
            return new IDataParameter[]{
                DataAccess.CreateParameter("@Id",t.Id),
                DataAccess.CreateParameter("@UserId",t.UserId),
                DataAccess.CreateParameter("@ProductId",t.ProductId),
                DataAccess.CreateParameter("@UserName",t.UserName),
                DataAccess.CreateParameter("@Content",t.Content),
                DataAccess.CreateParameter("@FeedBack",t.FeedBack)
            };
        }

        public int Insert(Model.CommentaryInfo commentary)
        {
            return builderDAL.Insert(
                commentary,
                "ProductId",
                "UserId",
                "UserName",
                "Content");
        }

        public bool Update(CommentaryInfo commentary)
        {
            return builderDAL.Update(
                commentary,
                "Id=@Id",
                "Content",
                "FeedBack");
        }

        public bool Delete(int commentary)
        {
            return builderDAL.Delete(commentary);
        }

        public CommentaryInfo GetCommentary(int id)
        {
            return builderDAL.GetModel(id);
        }

        public IList<CommentaryInfo> GetCommentaryByUser(int userId, int pageIndex, int pageSize, out int totalRecord)
        {
            return builderDAL.GetModels(
                "Commentary,Products",
                string.Empty,
                "Commentary.*,Products.Title as ProductName",
                "Commentary.ProductId=Products.Id and Commentary.UserId=@UserId",
                "Commentary.Id desc",
                pageIndex,
                pageSize,
                out totalRecord,
                DataAccess.CreateParameter("@UserId", userId));
        }

        public IList<CommentaryInfo> GetCommentary(int productId, int pageIndex, int pageSize, out int totalRecord)
        {
            string where = "Products.Id=Commentary.ProductId";

            where += productId > 0
              ? " and ProductId=@ProductId"
              : string.Empty;

            return builderDAL.GetModels(
               "Commentary,Products",
                string.Empty,
                "Commentary.*,Products.Title as ProductName",
                where,
                "Commentary.Id desc",
                pageIndex,
                pageSize,
                out totalRecord,
                DataAccess.CreateParameter("@ProductId", productId));
        }

        public IList<CommentaryInfo> GetCommentaryByPrdocutCategory(int productCategoryId, int pageIndex, int pageSize, out int totalRecord)
        {
            string where = "Commentary.ProductId=Products.Id";

            where += productCategoryId > 0
               ? " and exists (select ProductId from Products where ProductCategoryId=@ProductCategoryId and Commentary.ProductId=Products.Id)"
               : string.Empty;

            return builderDAL.GetModels(
                "Commentary,Products",
                string.Empty,
                "Commentary.*,Products.Title as ProductName",
                where,
                "Commentary.Id desc",
                pageIndex,
                pageSize,
                out totalRecord,
                DataAccess.CreateParameter("@ProductCategoryId", productCategoryId));
        }


        public IList<CommentaryInfo> GetCommentary(bool? isBack, int pageIndex, int pageSize, out int totalRecord)
        {
            string where = string.Empty;
            if(isBack!=null)
            {
                where=isBack.Value
                ? "FeedBack is not null"
                : "FeedBack is null";
            }

            return builderDAL.GetModels(
                "Commentary,Products",
                string.Empty,
                "Commentary.*,Products.Title as ProductName",
                where,
                "Commentary.Id desc",
                pageIndex,
                pageSize,
                out totalRecord);
        }
    }
}
